Attribute VB_Name = "ftp"
Option Explicit

Private Const INTERNET_OPEN_TYPE_DIRECT = 1
Private Const INTERNET_FLAG_PASSIVE = &H8000000

'config env
Private Declare Function InternetOpen Lib "wininet.dll" Alias "InternetOpenA" (ByVal sAgent As String, ByVal lAccessType As Long, ByVal sProxyName As String, ByVal sProxyBypass As String, ByVal lFlags As Long) As Long

'connect to server
Private Declare Function InternetConnect Lib "wininet.dll" Alias "InternetConnectA" (ByVal hInternetSession As Long, ByVal sServerName As String, ByVal nServerPort As Integer, ByVal sUserName As String, ByVal sPassword As String, ByVal lService As Long, ByVal lFlags As Long, ByVal lContext As Long) As Long

'upload
Private Declare Function FtpPutFile Lib "wininet.dll" Alias "FtpPutFileA" (ByVal hFtpSession As Long, ByVal lpszLocalFile As String, ByVal lpszRemoteFile As String, ByVal dwFlags As Long, ByVal dwContext As Long) As Boolean
Private Declare Function FtpGetFile Lib "wininet.dll" Alias "FtpGetFileA" (ByVal hFtpSession As Long, ByVal lpszRemoteFile As String, ByVal lpszNewFile As String, ByVal fFailIfExists As Boolean, ByVal dwFlagsAndAttributes As Long, ByVal dwFlags As Long, ByVal dwContext As Long) As Boolean
Private Declare Function InternetCloseHandle Lib "wininet.dll" (ByVal hInet As Long) As Integer

'upload file
Public Function UpLoadFile(sIp As String, sRemoteFileName As String, sLocalFileName As String, sUserName As String, sPassword As String) As Boolean
    Dim ret As Boolean
    Dim hOpen As Long
    Dim hConnection As Long
    
    hOpen = InternetOpen(vbNullString, INTERNET_OPEN_TYPE_DIRECT, vbNullString, vbNullString, 0)
    If hOpen <> 0 Then
        hConnection = InternetConnect(hOpen, sIp, 0, sUserName, sPassword, 1, INTERNET_FLAG_PASSIVE, 0)
        If hConnection <> 0 Then 'connection ok
            ret = FtpPutFile(hConnection, sLocalFileName, sRemoteFileName, 2, 0)
            UpLoadFile = ret
            MsgBox "Upload successfully!", vbInformation
        Else 'cant connect
            UpLoadFile = False
            MsgBox "user name, password or network error!", vbCritical
        End If
    Else
        UpLoadFile = False
    End If
    
    InternetCloseHandle hConnection
    InternetCloseHandle hOpen
End Function

'FTP download
Public Function DownLoadFile(sIp As String, sRemoteFileName As String, sLocalFileName As String, sUserName As String, sPassword As String)
    Dim ret As Boolean
    Dim hOpen As Long
    Dim hConnection As Long
    
    hOpen = InternetOpen(vbNullString, INTERNET_OPEN_TYPE_DIRECT, vbNullString, vbNullString, 0)
    If hOpen <> 0 Then
        hConnection = InternetConnect(hOpen, sIp, 0, sUserName, sPassword, 1, INTERNET_FLAG_PASSIVE, 0)
        If hConnection <> 0 Then 'connection ok
            ret = FtpGetFile(hConnection, sRemoteFileName, sLocalFileName, 0, 0, 1, 0)
            DownLoadFile = ret
            MsgBox "Download successfully!", vbInformation
        Else 'can not connect
            DownLoadFile = False
            MsgBox "user name, password or network error!", vbCritical
        End If
    Else
        DownLoadFile = False
    End If
    InternetCloseHandle hConnection
    InternetCloseHandle hOpen
End Function

Private Sub cmdDownload_Click()
  'DownLoadFile "10.182.35.161", "/data/app1/xxx/obdtester_svr/obdtester_svr.def", "C:\obdtester_svr.def", "user", "12345"
End Sub

Private Sub cmdUpload_Click()
  'UpLoadFile "10.182.35.161", "/data/app1/xxx/work/products/openboss/busiaccep_svr/aaatest/myboot.ini", "C:\boot.ini", "user", "12345"
End Sub
